<%@page import="org.springframework.security.core.context.SecurityContextHolder"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mg" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<a href="#" class="dropdown-toggle" data-toggle="dropdown">
    <i class="icon-user icon-white"></i>
    <sec:authorize access="isAuthenticated()">
        <%=SecurityContextHolder.getContext().getAuthentication().getName()%>
    </sec:authorize>
    <sec:authorize access="isAnonymous()">
        Anonymous
    </sec:authorize>
    <b class="caret"></b>
</a>
<ul class="dropdown-menu">
    <sec:authorize access="isAnonymous()">
        <script>
            if(sessionCountdown) sessionCountdown.stop()
            
            function quicklogin(){
                login({
                    login: $('#quick-login'),
                    password: $('#quick-password'), 
                    url: '<mg:baseurl/>'
                })
            }
            
            function loginKeyupHandler(e){
                if(e.keyCode==13) quicklogin();
            }
        </script>
        <li class="nav-header">
            Quick Log In
            <input id="quick-login" type="text" autocomplete="on" placeholder="Login">
            <div class="input-append">
                <input style="vertical-align: bottom; width: 142px;" id="quick-password" type="password" placeholder="Password">
                <div style="text-transform: initial;" class="btn btn-primary" onclick="quicklogin()">Login</div>
            </div>
            <script>
                $('#quick-login').bind('keyup', loginKeyupHandler);
                $('#quick-password').bind('keyup', loginKeyupHandler);
            </script>
        </li>
        <li class="divider"></li>
        <li><a href="<mg:url value="login"/>">Log In</a></li>
        <li><a href="<mg:url value="registration"/>">Registration</a></li>
    </sec:authorize>
    <sec:authorize access="isAuthenticated()">
        <li class="nav-header">My content</li>
        <li><a href="#">Poetry</a></li>
        <li><a href="#">Music</a></li>
        <li class="divider"></li>
        <li onclick="logout('<mg:baseurl/>')"><a>Log Out&nbsp;<i class="icon-off"></i>&nbsp;<span id="session_counter"></span></a></li>
        <script>
            sessionCountdown = new countdown({
                target: $('#session_counter'),
                time: 30*60,
                callback: function(){
                    logout('<mg:baseurl/>');
                },
                warning: {
                    time: 45,
                    message: 'Time of session will expire soon.',
                    color: 'red'
                }
            });
        </script>
    </sec:authorize>
</ul>